package com.lancoo.znbkxx.base;

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * Create by TangJun on 2019/2/16
 */
public class AppActivityManager {
    private static final AppActivityManager a = new AppActivityManager();
    private static final List<AppCompatActivity> mActivities = new ArrayList<>();

    public static AppActivityManager getInstance() {
        return a;
    }

    public void addActivity(AppCompatActivity activity) {
        if (activity != null) {
            mActivities.add(activity);
        }
    }

    public AppCompatActivity getTopActivity() {
        if (mActivities.size() == 0) {
            return null;
        }
        return mActivities.get(mActivities.size() - 1);
    }


    public void removeActivity(AppCompatActivity activity) {
        if (activity != null && mActivities.contains(activity)) {
            mActivities.remove(activity);
        }
    }


    public void finishActivity(AppCompatActivity activity) {
        if (activity != null && mActivities.contains(activity)) {
            removeActivity(activity);
            activity.finish();
        }
    }
    public void popActivity(AppCompatActivity activity) {
        removeActivity(activity);
        activity.finish();
    }
    /**
     * 完全退出
     */
    public void exit() {
        while (mActivities.size() > 0) {
            popActivity(mActivities.get(mActivities.size() - 1));
        }
        System.exit(0);
    }
    public void finishAllActivity() {
        for (Activity a : mActivities) {
            a.finish();
        }
        mActivities.clear();
    }

    public int getActivitySize() {
        return mActivities.size();
    }

    public List<AppCompatActivity> getActivities() {
        return mActivities;
    }
}
